package com.anyonavinfo.cpaddemo.data.hostinfo.source.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.anyonavinfo.cpaddemo.Application;
import com.autopet.hardware.aidl.IHWCallBack;
import com.autopet.hardware.aidl.IHWSendCmd;

import static android.content.Context.BIND_AUTO_CREATE;

/**
 * Created by ZuiC on 2017/2/1.
 */

public class E3HWServiceManager {

    private IHWSendCmd E3HWService;
    private Context mContext;
    private static E3HWServiceManager INSTANCE = null;

    public static E3HWServiceManager getInstance(){
        if (INSTANCE == null) {
            INSTANCE = new E3HWServiceManager();
        }
        return INSTANCE;
    }

    private E3HWServiceManager(){
        mContext = Application.getContextObject();
        startService();
    }

    private void startService() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.E3HWService");
        intent.setPackage("com.autopet.hardware.aidl");
        mContext.startService(intent);
        bindService();
        E3HWServiceHelper.getServiceHelperInstance().setOnJsonOutput(new E3HWServiceHelper.OnJsonOutput() {
            @Override
            public void outputjson(String jsonstr) {
                try {
                    if (E3HWService != null) {
                        E3HWService.sendCommand(jsonstr);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public void endService() {

        if (connBase != null) {
            mContext.unbindService(connBase);
        }
        try {
            if (E3HWService != null) {
                E3HWService.unregisterCallback(ihwcb);
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        E3HWService = null;
    }

    private ServiceConnection connBase = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            E3HWService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            E3HWService = IHWSendCmd.Stub.asInterface(service);
            try {
                E3HWService.registerCallback(ihwcb);
                E3HWServiceHelper.getServiceHelperInstance().ExecCommand(E3HWServiceHelper.getServiceHelperInstance().INIT_RADIOINFO);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };


    private void bindService() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.E3HWService");
        intent.setPackage("com.autopet.hardware.aidl");
        mContext.bindService(intent, connBase, BIND_AUTO_CREATE);
    }


    private IHWCallBack ihwcb = new IHWCallBack.Stub() {
        @Override
        public void updateStatus(String NewStatus) {
            E3HWServiceHelper.getServiceHelperInstance().updateServiceState(NewStatus);
        }
    };

<<<<<<< HEAD
=======
    public String getLongitudeAndLatitude(){
        return null;
    }
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
}
